package com.alphacsp.android;

import android.app.Activity;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
 * @author Yoav Aharoni
 */
public class FighterView {
	private final ImageView imageView;
	private final TextView nameView;
	private final ProgressBar powerView;
	private final Animation punchAnimation;
	private final Animation hitAnimation;

	public FighterView(int viewId, boolean leftSide, Activity activity) {
		this((ViewGroup) activity.findViewById(viewId), leftSide);
	}

	public FighterView(ViewGroup view, boolean leftSide) {
		// get views
		nameView = (TextView) view.findViewById(R.id.fighterName);
		imageView = (ImageView) view.findViewById(R.id.fighterImage);
		powerView = (ProgressBar) view.findViewById(R.id.fighterPower);

		// init animations
		final int punchResource = leftSide ? R.anim.punch_left : R.anim.punch_right;
		punchAnimation = AnimationUtils.loadAnimation(view.getContext(), punchResource);
		hitAnimation = AnimationUtils.loadAnimation(view.getContext(), R.anim.hit);
	}

	public ImageView getImageView() {
		return imageView;
	}

	public void setName(String name) {
		nameView.setText(name);
	}

	public String getName() {
		return nameView.getText().toString();
	}

	public void setPower(int power) {
		powerView.setProgress(power);
	}

	public int getPower() {
		return powerView.getProgress();
	}

	public void takeHit() {
		final int power = getPower();
		final int hitPower = (int) (Math.random() * 20);
		setPower(power - hitPower);
		imageView.startAnimation(hitAnimation);
	}

	public void punch() {
		imageView.startAnimation(punchAnimation);
	}
}
